<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/yuwanzi.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/yuwanzi.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/yuwanzi.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/yuwanzi.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/yuwanzi.io/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"suyuhuan.gitee.io","root":"/yuwanzi.io/","images":"/yuwanzi.io/images","scheme":"Muse","version":"8.2.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Suche...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="Chromium如何显示页面原文地址 译文地址 文档从下向上讲述了Chromium怎样显示网页. 确保你看过multi-process architecture设计文档了. 理解主要组件的阻塞流程很重要. multi-process resource loading这一篇讲怎样从互联网上获取页面可能也有帮助.  概念上的应用层  每个方块代表一个应用层. 应用层不应该知道或者依赖更高层的应用.">
<meta property="og:type" content="article">
<meta property="og:title" content="How-Chromium-Displays-Web-Pages">
<meta property="og:url" content="https://suyuhuan.gitee.io/yuwanzi.io/2019/07/27/2019-07-27-How-Chromium-Displays-Web-Pages/index.html">
<meta property="og:site_name" content="玉丸子 | Blog">
<meta property="og:description" content="Chromium如何显示页面原文地址 译文地址 文档从下向上讲述了Chromium怎样显示网页. 确保你看过multi-process architecture设计文档了. 理解主要组件的阻塞流程很重要. multi-process resource loading这一篇讲怎样从互联网上获取页面可能也有帮助.  概念上的应用层  每个方块代表一个应用层. 应用层不应该知道或者依赖更高层的应用.">
<meta property="og:locale">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190810152034.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190810152243.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190810152635.png">
<meta property="article:published_time" content="2019-07-27T00:25:11.000Z">
<meta property="article:modified_time" content="2020-11-07T00:58:17.000Z">
<meta property="article:author" content="玉丸子">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="浏览器">
<meta property="article:tag" content="2019">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190810152034.png">


<link rel="canonical" href="https://suyuhuan.gitee.io/yuwanzi.io/2019/07/27/2019-07-27-How-Chromium-Displays-Web-Pages/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh'
  };
</script>
<title>How-Chromium-Displays-Web-Pages | 玉丸子 | Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/yuwanzi.io/atom.xml" title="玉丸子 | Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Navigationsleiste an/ausschalten" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/yuwanzi.io/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">玉丸子 | Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Inhaltsverzeichnis
        </li>
        <li class="sidebar-nav-overview">
          Übersicht
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Chromium%E5%A6%82%E4%BD%95%E6%98%BE%E7%A4%BA%E9%A1%B5%E9%9D%A2"><span class="nav-number">1.</span> <span class="nav-text">Chromium如何显示页面</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A6%82%E5%BF%B5%E4%B8%8A%E7%9A%84%E5%BA%94%E7%94%A8%E5%B1%82"><span class="nav-number">1.1.</span> <span class="nav-text">概念上的应用层</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Webkit"><span class="nav-number">1.2.</span> <span class="nav-text">Webkit</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#The-WebKit-port"><span class="nav-number">1.2.1.</span> <span class="nav-text">The WebKit port</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#The-WebKit-glue"><span class="nav-number">1.3.</span> <span class="nav-text">The WebKit glue</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B8%B2%E6%9F%93%E8%BF%9B%E7%A8%8B"><span class="nav-number">1.4.</span> <span class="nav-text">渲染进程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B8%B2%E6%9F%93%E5%99%A8%E7%9A%84%E7%BA%BF%E7%A8%8B"><span class="nav-number">1.4.1.</span> <span class="nav-text">渲染器的线程</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E8%BF%9B%E7%A8%8B"><span class="nav-number">1.5.</span> <span class="nav-text">浏览器进程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BA%95%E5%B1%82%E6%B5%8F%E8%A7%88%E5%99%A8%E8%BF%9B%E7%A8%8B%E5%AF%B9%E8%B1%A1"><span class="nav-number">1.5.1.</span> <span class="nav-text">底层浏览器进程对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%8A%E5%B1%82%E6%B5%8F%E8%A7%88%E5%99%A8%E8%BF%9B%E7%A8%8B%E5%AF%B9%E8%B1%A1"><span class="nav-number">1.5.2.</span> <span class="nav-text">上层浏览器进程对象</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%AF%B4%E6%98%8E%E7%94%A8%E4%BE%8B%E5%AD%90"><span class="nav-number">1.6.</span> <span class="nav-text">说明用例子</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E2%80%9Cset-cursor%E2%80%9D%E6%B6%88%E6%81%AF%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="nav-number">1.6.1.</span> <span class="nav-text">“set cursor”消息的生命周期</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E2%80%9Cmouse-click%E2%80%9D%E6%B6%88%E6%81%AF%E7%9A%84%E5%91%A8%E6%9C%9F"><span class="nav-number">1.6.2.</span> <span class="nav-text">“mouse click”消息的周期</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">玉丸子</p>
  <div class="site-description" itemprop="description">这里是玉丸子的个人博客,与你一起发现更大的世界。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/yuwanzi.io/archives">
          <span class="site-state-item-count">68</span>
          <span class="site-state-item-name">Artikel</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/yuwanzi.io/categories/">
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">Kategorien</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/yuwanzi.io/tags/">
        <span class="site-state-item-count">46</span>
        <span class="site-state-item-name">schlagwörter</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh">
    <link itemprop="mainEntityOfPage" href="https://suyuhuan.gitee.io/yuwanzi.io/2019/07/27/2019-07-27-How-Chromium-Displays-Web-Pages/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/yuwanzi.io/images/avatar.gif">
      <meta itemprop="name" content="玉丸子">
      <meta itemprop="description" content="这里是玉丸子的个人博客,与你一起发现更大的世界。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="玉丸子 | Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          How-Chromium-Displays-Web-Pages
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Veröffentlicht am</span>

      <time title="Erstellt: 2019-07-27 08:25:11" itemprop="dateCreated datePublished" datetime="2019-07-27T08:25:11+08:00">2019-07-27</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Bearbeitet am</span>
        <time title="Geändert am: 2020-11-07 08:58:17" itemprop="dateModified" datetime="2020-11-07T08:58:17+08:00">2020-11-07</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">in</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%89%8D%E7%AB%AF/%E6%B5%8F%E8%A7%88%E5%99%A8/" itemprop="url" rel="index"><span itemprop="name">浏览器</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Chromium如何显示页面"><a href="#Chromium如何显示页面" class="headerlink" title="Chromium如何显示页面"></a>Chromium如何显示页面</h1><p><a target="_blank" rel="noopener" href="http://www.chromium.org/developers/design-documents/displaying-a-web-page-in-chrome">原文地址</a></p>
<p><a target="_blank" rel="noopener" href="https://github.com/nifei/memo/blob/master/Chrome/How-Chromium-Displays-Web-Pages.md">译文地址</a></p>
<p>文档从下向上讲述了Chromium怎样显示网页. 确保你看过<a target="_blank" rel="noopener" href="http://www.chromium.org/developers/design-documents/multi-process-architecture">multi-process architecture</a>设计文档了. 理解主要组件的阻塞流程很重要. <a target="_blank" rel="noopener" href="http://www.chromium.org/developers/design-documents/multi-process-resource-loading">multi-process resource loading</a>这一篇讲怎样从互联网上获取页面可能也有帮助. </p>
<h2 id="概念上的应用层"><a href="#概念上的应用层" class="headerlink" title="概念上的应用层"></a>概念上的应用层</h2><p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190810152034.png" alt="alt text" title="(The original Google Doc for this illustration is http://goo.gl/MsEJX which is open for editing by any @chromium.org)"> </p>
<p>每个方块代表一个应用层. 应用层不应该知道或者依赖更高层的应用. </p>
<ul>
<li><p><strong>Webkit:</strong> 在Safari, Chromium和其他基于Webkit的浏览器中使用的渲染引擎. <strong>Port</strong>是Webkit的一部分, 它整合平台相关的系统服务, 像资源加载或者图片什么的. </p>
</li>
<li><p><strong>Glue:</strong> Webkit类型到Chromium类型的转换. 这是”Webkit嵌套层”. 这是Chromium和test_shell(测试Webkit用的)这两个浏览器的基础. </p>
</li>
<li><p><strong>Renderer/Render Host:</strong> Chromium的”多进程嵌入层”. 它在进程边检之间代理通知和指令. </p>
</li>
<li><p><strong>WebContents:</strong> 内容模块的可重用组件, 主类. 可嵌套, 允许Html到view的多进程渲染. 详见<a target="_blank" rel="noopener" href="http://www.chromium.org/developers/content-module">Content module pages</a>. </p>
</li>
<li><p><strong>Brwoser:</strong> 浏览器窗口, 包括多个WebContentses. </p>
</li>
<li><p><strong>Tab Helpers:</strong> 可以附加到WebContents上的独立对象(通过WebContentsUserData mixin). 浏览器往它持有的WebContentses(一个网站图标, 一个信息栏, 等)上附加一个这些玩意的混搭. </p>
</li>
</ul>
<h2 id="Webkit"><a href="#Webkit" class="headerlink" title="Webkit"></a>Webkit</h2><p>我们用<a target="_blank" rel="noopener" href="http://webkit.org/">Webkit</a>开源工程来排版网页. 代码从Apple拉过来放在路径<code>/third_party/WebKit</code>下. Webkit主要是由代表核心排版功能的”WebCore”和运行JavaScript的”JavaScriptCore”组成. 我们只用JavaScriptCore来跑测试, 通常使用性能更好的V8 JavaScrip引擎来代替它. 我们实际上并不使用苹果称为”WebKit”的那一层, 那个是WebCore和诸如Safari的OS X应用的嵌入式API. 为了方便我们一般把苹果的代码称作”WebKit”. </p>
<h3 id="The-WebKit-port"><a href="#The-WebKit-port" class="headerlink" title="The WebKit port"></a>The WebKit port</h3><p>在最底层我们有我们的WebKit “port”. 这是需要的特定平台和平台独立的WebCore代码交互的功能的实现. 文件在WebKit tree中, 通常在<code>chromium</code>路径或者Chromium后缀的文件. 端口(port)的大部分不限定OS: 可以把它想成WebCore的”Chromium端口”. 有些部分例如字体渲染必需根据平台分别处理. </p>
<ul>
<li><p>网络通信是由<a target="_blank" rel="noopener" href="http://www.chromium.org/developers/design-documents/multi-process-resource-loading">multi-process resource loading</a>系统处理的, 而不是从渲染进程直接移交操作系统. </p>
</li>
<li><p>图片使用Android开发的Skia图片库. 这个跨平台的图片库处理所有图片和图片权限, 除了文字. Skia放在<code>/third_party/skia</code>下. 图片操作的切入点是<code>/webkit/port/platform/graphics/GraphicsContextSkia.cpp</code>. 它会用到同路径下和<code>/base/gfx</code>下的其他文件.</p>
</li>
</ul>
<h2 id="The-WebKit-glue"><a href="#The-WebKit-glue" class="headerlink" title="The WebKit glue"></a>The WebKit glue</h2><p>Chromium应用使用和第三方WebKit代码不同的类型, 编码风格和代码排版. WebKit “glue” 使用Google代码风格和类型提供了更方便的嵌入API(例子, 用<code>std::string</code>代替<code>WebCore::string</code>, <code>GURL</code>代替<code>KURL</code>). 黏合代码(glue code)置于<code>/webkit/glue</code>. 黏合对象和WebKit对象命名通常类似, 以”Web”打头. 例如<code>WebCore::Frame</code>变成<code>WebFrame</code>. </p>
<p>WebKit”黏合”层把Chromium代码基础的其他部分和WebCore的数据类型隔离开来, 将WebCore的变化对Chromium代码基础(基线?)的影响降到最小. Chromium从不直接使用WebCore的数据类型. 加到WebKit的API在需要调用某个WebCore对象的时候为了Chromium黏合. </p>
<p>“test shell”应用是个用于测试WebKit端口和黏合代码的极简的浏览器. 它像chromium一样使用相同的glue接口和WebKit通信. 开发者不必使用复杂的浏览器特性, 线程和进程就可以测试新代码. 这个应用也用来跑WebKit的自动化测试. 但是”test shell”不像Chromium一样以多进程的方式使用WebKit. 内容模块被放置在一个叫做”content shell”的应用中立即跑测试. </p>
<h2 id="渲染进程"><a href="#渲染进程" class="headerlink" title="渲染进程"></a>渲染进程</h2><p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190810152243.png" alt="alt text"></p>
<p>Chromium的渲染进程使用黏合接口嵌入在WebKit端口中. 代码不多: 主要任务是作为连接浏览器的IPC通道的渲染器端. </p>
<p>渲染器中最重要的类是<code>RenderView</code>, 在<code>/content/renderer/render_view_impl.cc</code>. 这个对象代表网页. 它处理所有来往浏览器进程的跳转相关的指令. 它继承自<code>RenderWidget</code>, <code>RenderWidget</code>提供绘制和输入事件处理. <code>RenderView</code>通过全局的<code>RenderProcess</code>对象(每个渲染进程)和浏览器进程通信. </p>
<p><strong>FAQ:RenderWidget和RenderView的区别?</strong> <code>RenderWidget</code>通过实现黏合层叫做<code>WebWidgetDelegate</code>的抽象接口映射到<code>WebCore::Widget</code>对象. 基本上就是一个接收输入事件,我们要往上绘制的窗口. <code>RenderView</code>继承自<code>RenderWidget</code>, 是窗口或页面的内容. <code>RenderView</code>除了绘制和接收输入事件之外还要处理跳转指令. 只有一种情况下一个<code>RenderWidget</code>不是一个<code>RenderView</code>: 网页上的选择框. 那种有一个向下的箭头, 会弹出来一个选择列表的框框(combo box). 选择框需要使用原生窗口渲染以保证在最顶层, 还可能超过窗口边界. 这样的窗口也需要绘制和接收事件但是并不是一个”网页”(<code>RenderView</code>). </p>
<h3 id="渲染器的线程"><a href="#渲染器的线程" class="headerlink" title="渲染器的线程"></a>渲染器的线程</h3><p>一个渲染器有两个线程(见<a target="_blank" rel="noopener" href="http://www.chromium.org/developers/design-documents/multi-process-architecture">multi-process architecture</a>的流程图, 或者见<a target="_blank" rel="noopener" href="http://www.chromium.org/developers/design-documents/threading">threading in Chromium</a>怎样编程). 主要对象像<code>RenderView</code>和所有的WebKit 代码会跑在渲染线程中. 在所有其他事情之中这允许我们同步地从渲染器向浏览器发送消息. 少数几个操作要求来自浏览器的结果要继续进行. 一个例子是JavaScript要求的时候获取页面cookie. 渲染器线程会阻塞, 主线程排队处理所有的消息直到找到正确的回应. 这期间受到的所有消息会按顺序放在渲染器线程的后面. </p>
<h2 id="浏览器进程"><a href="#浏览器进程" class="headerlink" title="浏览器进程"></a>浏览器进程</h2><p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190810152635.png" alt="alt text"></p>
<h3 id="底层浏览器进程对象"><a href="#底层浏览器进程对象" class="headerlink" title="底层浏览器进程对象"></a>底层浏览器进程对象</h3><p>所有和渲染器进程之间的<a target="_blank" rel="noopener" href="http://www.chromium.org/developers/design-documents/inter-process-communication">IPC</a>通信都是在浏览器的I/O线程中完成的. 这个线程还处理避免介入UI的<a target="_blank" rel="noopener" href="http://www.chromium.org/developers/design-documents/multi-process-resource-loading">network communication</a>. </p>
<p><code>RenderProcessHost</code>在主线程(UI线程)中初始化的时候会创建新的渲染器进程和带有通到渲染器的命名的管道<code>ChannelProxy</code>IPC对象. 这个对象(ChannelProxy)在浏览器的I/O线程中运行, 监听渲染器的通道, 自动把所有消息发送回UI线程的<code>RenderprocessHost</code>. 这个通道(ChannelProxy)上会安装一个<code>ResourceMessageFilter</code>, 滤掉能在I/O线程中直接处理的常规信息像网络请求什么的. 过滤的实现在<code>ResourceMessageFilter::OnMessageReceived</code>. </p>
<p>UI线程的<code>RenderProcessHost</code>负责分配每个view的信息到合适的<code>RenderViewHost</code>(介个会自己处理一些和view没有关系的信息)去. 消息分配实现在<code>RenderProcessHost::OnMessageReceived</code>. </p>
<h3 id="上层浏览器进程对象"><a href="#上层浏览器进程对象" class="headerlink" title="上层浏览器进程对象"></a>上层浏览器进程对象</h3><p>具体的view的消息会被传到<code>RenderViewHost::OnMessageReceived</code>. 多数消息在此处理, 其他的在基类<code>RenderWidgetHOST</code>中处理. 这两个对应渲染器中的<code>RenderView</code>和<code>RenderWidget</code>(见上面的”The Render Process”). 每个平台有一个view的类(<code>RenderWidgetHostView[Aura|Gtk|Mac|Win]</code>)来实现原生的view系统的整合. </p>
<p>在<code>RenderView/Widget</code>上层是<code>WebContents</code>对象, 多数消息最终由这个对象的一个函数调用来处理. <code>WebContents</code>表现网页内容. 是内容模块的顶层对象, 负责在一个矩形视图中显示网页. 详见<a target="_blank" rel="noopener" href="http://www.chromium.org/developers/content-module">content module</a>. </p>
<p><code>WebContents</code>对象包含在<code>TabContentsWrapper</code>中. 在<code>chrome/</code>下面, 负责一个tab. </p>
<h2 id="说明用例子"><a href="#说明用例子" class="headerlink" title="说明用例子"></a>说明用例子</h2><p>其他的跳转和启动的例子在<a target="_blank" rel="noopener" href="http://www.chromium.org/developers/how-tos/getting-around-the-chrome-source-code">Getting Around the Chromium Source Code</a>中. </p>
<h3 id="“set-cursor”消息的生命周期"><a href="#“set-cursor”消息的生命周期" class="headerlink" title="“set cursor”消息的生命周期"></a>“set cursor”消息的生命周期</h3><p>设置鼠标是从渲染器发送到浏览器的典型消息的一个例子. 在渲染器中, 发生了这些事情. </p>
<ul>
<li><p>Set cursor 消息由WebKit内部生成, 通常是用来相应输入事件. Set cursor消息始于<code>RenderWidget::SetCursor in content/renderer/render_widget.cc</code>. </p>
</li>
<li><p>(Set Cursor消息)会调用<code>RenderWidget::Send</code>来分配消息. <code>RenderView</code>也用这个方法来向浏览器发送消息, 它会调用<code>RenderThread::Send</code>. </p>
</li>
<li><p>会调到<code>IPC::SyncChannel</code>, 函数内部会代理发送这个消息到渲染器的主线程, 放在浏览器命名通道的后面. </p>
</li>
</ul>
<p>然后浏览器会接管过来: </p>
<ul>
<li><p><code>RenderProcessHost</code>中的<code>IPC::ChannelProxy</code>会接收到所有浏览器I/O线程的消息. 它会先把消息发送到<code>ResourceMessageFilter</code>分派网络请求和I/O线程的直接相关消息. 因为Set Cursor这个消息不会被过滤掉, 它会继续发送到浏览器的UI线程(<code>IPC;ChannelProxy</code>会在内部处理). </p>
</li>
<li><p> 在<code>content/browser/renderer_host/render_process_host_impl.cc</code>中<code>RenderProcessHost::OnMessageReceived</code>拿到相应渲染进程的所有view的消息. 它会直接处理几种类型的消息, 剩下的分发到和发送消息的<code>RenderView</code>相应的<code>RenderViewHost</code>上. </p>
</li>
<li><p><code>RenderViewHost</code>没有处理的消息都会发送到<code>RenderWidgetHost</code>中处理, 包括Set cursor消息. </p>
</li>
<li><p><code>content/browser/renderer_host/render_view_host_impl.cc</code>中的消息表最终在<code>RenderWidgetHost::OnMsgSetCursor</code>中收到消息, 调用相应的UI函数来设置鼠标. </p>
</li>
</ul>
<h3 id="“mouse-click”消息的周期"><a href="#“mouse-click”消息的周期" class="headerlink" title="“mouse click”消息的周期"></a>“mouse click”消息的周期</h3><p>发送鼠标点击是从浏览器发送到渲染器的典型消息. </p>
<ul>
<li><p>浏览器的UI线程通过<code>RenderWidgetHostViewWin::OnMouseEvent</code>收到Windows消息, 然后调用同一个类里的<code>ForwardMouseEventToRenderer </code>. </p>
</li>
<li><p>发送函数将输入事件打包成一个跨平台的<code>WebMouseEvent</code>最后发送到相关的<code>RenderWidgetHost</code>. </p>
</li>
<li><p><code>RenderWidgetHost::ForwardInputEvent</code>创建一个IPC消息<code>ViewMsg_HandleInputEvent</code>, 转播<code>WebInputEvent</code>给它, 然后调用<code>RenderWidgetHost::Send</code>. </p>
</li>
<li><p>这样做只是转播<code>RenderProcessHost::Send</code>函数, <code>RenderProcessHost::Send</code>会按顺序发送消息到<code>IPC::ChannelProxy</code>. </p>
</li>
<li><p>在内部<code>IPC::ChannelProxy</code>把消息托管发送到浏览器的I/O线程, 并且写到对应渲染器的命名管道中. </p>
</li>
</ul>
<p>注意<code>WebContents</code>会创建许多其他类型的消息, 特别是跳转一类的. 这些消息通过类似的途径从<code>WebContents</code>传递到<code>RenderViewHost</code>. </p>
<p>渲染器接管: </p>
<ul>
<li><p>渲染器主线程中的<code>IPC::Chanel</code>读取浏览器发送的消息, <code>IPC::ChannelProxy</code>把消息委托到渲染器线程. </p>
</li>
<li><p><code>RenderView::OnMessageReceived</code>拿到消息. 很多种消息在此直接处理. click消息不是, 它会(和其他未被处理的消息一起)发送到<code>RenderWidget::OnMessageReceived</code>, 后者按顺序发送到<code>RenderWidget::OnHandelInputEvent</code>. </p>
</li>
<li><p>输入事件被发送到<code>WebWidgetImpl::HandleInputEvent</code>, <code>WebWidgetImpl::HandleInputEvent</code>把它转换成一个WebKit<code>PlatformMouseEvent</code>类型然后发送给WebKit离得<code>WebCore::Widget</code>类. </p>
</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/yuwanzi.io/tags/%E5%89%8D%E7%AB%AF/" rel="tag"># 前端</a>
              <a href="/yuwanzi.io/tags/%E6%B5%8F%E8%A7%88%E5%99%A8/" rel="tag"># 浏览器</a>
              <a href="/yuwanzi.io/tags/2019/" rel="tag"># 2019</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2018/03/16/2018-03-16-map_family/" rel="prev" title="Map大家族的那点事儿">
                  <i class="fa fa-chevron-left"></i> Map大家族的那点事儿
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2019/08/10/2019-08-10-How-Blink-Works-%E4%B8%AD%E6%96%87%E8%AF%91%E6%96%87/" rel="next" title="How Blink Works 中文译文">
                  How Blink Works 中文译文 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">玉丸子</span>
</div>
  <div class="powered-by">Erstellt mit  <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/yuwanzi.io/js/utils.js"></script><script src="/yuwanzi.io/js/motion.js"></script><script src="/yuwanzi.io/js/schemes/muse.js"></script><script src="/yuwanzi.io/js/next-boot.js"></script>

  






  





</body>
</html>
